import tkinter as tk
from tkinter import filedialog
import re
import pandas as pd
import datetime
import requests
from lxml import etree
import json
import akshare as ak
import os
import efinance as ef


# 全局 #########
os.chdir(os.path.dirname(os.path.abspath(__file__)))
today = datetime.date.today().strftime('%Y%m%d')
cookie = "xxx"
headers = {'cookie': cookie, "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}

# stock_zh_a_spot_em_df = ak.stock_zh_a_spot()
# stock_zh_a_spot_em_df.to_excel('all_gp.xlsx')
result = pd.read_excel("all_gp.xlsx")
# 全局 #########



def input_file_path():
    window = tk.Tk()
    window.title('(^_^))')
    window.geometry('220x20')
    tk.Label(window, text='"Pls Select The File"').pack()
    inpath = filedialog.askopenfilename()
    window.destroy()
    return inpath

def input_folder_path():
    window = tk.Tk()
    window.title('(^_^))')
    window.geometry('220x20')
    tk.Label(window, text='"Pls Select The File"').pack()
    inpath = filedialog.askdirectory()
    window.destroy()
    return inpath


# 查看雪球Top n热度的股票
def list_gp(n):
    url = 'https://stock.xueqiu.com/v5/stock/hot_stock/list.json'
    param = {
        'size': n,
        '_type': 10,
        'type': 10
    }

    page_text = requests.get(url=url, headers=headers, params=param).text
    page_text = json.loads(page_text)
    lst = [ (x['code'],x['name']) for x in page_text["data"]["items"]]
    print(lst)
    return lst


# 增加股票自选股，gp的格式要这样'SZ300199'
def add_gp(gp):
    url = 'https://stock.xueqiu.com/v5/stock/portfolio/stock/add.json'
    data = {
        'symbols': gp,
        'category': 1
    }
    response = requests.post(url=url, data=data, headers=headers)
    response = json.loads(response.text)
    if response['error_code'] == 0:
        print(gp +' 股票添加成功')
    else:
        print('股票添加失败,请检查')

# 删除股票
def delete_gp(gp):
    url = 'https://stock.xueqiu.com/v5/stock/portfolio/stock/cancel.json'
    data = {
        'symbols': gp
    }
    response = requests.post(url=url, data=data, headers=headers)
    response = json.loads(response.text)
    if response['error_code'] == 0:
        print(gp +' 股票删除成功')
    else:
        print('股票删除失败,请检查')

# 增加一个组
def add_gp_group(name):
    url = 'https://stock.xueqiu.com/v5/stock/portfolio/create.json'
    data = {
        'category': 1,
        'pnames': name
    }
    response = requests.post(url=url, data=data, headers=headers)
    response = json.loads(response.text)
    if response['error_code'] == 0:
        print(name + ' 组添加成功')
    else:
        print('组添加失败,请检查')

# 将股票加入到组里面，如果要加入到多个组，可以写多个
def add_gp_to_group(gp, name):
    url = 'https://stock.xueqiu.com/v5/stock/portfolio/stock/modify_portfolio.json'
    data = {
        'symbols': gp,
        'pnames': name,  # 如果多个组,可以写成 'xx1, xx2'
        'category': 1
    }

    response = requests.post(url=url, data=data, headers=headers)
    response = json.loads(response.text)
    if response['error_code'] == 0:
        print(gp + ' 添加到组 ' + name + ' 成功')
    else:
        print('股票添加组失败,请检查')